﻿
using System;
using SqlSugar;
using System.Threading.Tasks;
using System.Linq.Expressions;

using Oliving.Edge.Model;
using Oliving.Edge.Model.Dto;
using Oliving.Edge.Model.Models;
using Oliving.Edge.IRepository;
using Oliving.Edge.IRepository.UnitOfWork;
using Oliving.Edge.Repository.Base;
using Oliving.Edge.Model.ViewModels.ReportDto;
using System.Collections.Generic;

namespace Oliving.Edge.Repository
{
    /// <summary>
    /// DTSN_CanningRepository
    /// </summary>
    public class DTSN_CanningRepository : BaseRepository<DTSN_Canning>, IDTSN_CanningRepository
    {
        public DTSN_CanningRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }

        /// <summary>
        /// 自定义分页查询
        /// </summary>
        /// <param name="whereExpression">查询条件</param>
        /// <param name="pageIndex">页码（下标0）</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns></returns>
        public async Task<PageModel<DTSN_CanningPageDto>> QueryPageList(
            Expression<Func<DTSN_Canning, bool>> whereExpression,
            int pageIndex = 1,
            int pageSize = 20,
            string sortField = null,
            string sortOrder = null)
        {
            #region 排序

            string strOrderByFileds = null;

            if (!string.IsNullOrEmpty(sortField))
            {
                strOrderByFileds = sortField;
                if (sortOrder != null && sortOrder.ToLower().Contains("desc"))
                {
                    strOrderByFileds = strOrderByFileds + " desc";
                }
                else
                {
                    strOrderByFileds = strOrderByFileds + " asc";
                }
            }

            #endregion

            if (false)
            {
                #region 单表

                RefAsync<int> totalCount = 0;
                var list = await Db.Queryable<DTSN_Canning>()
                 .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                 .WhereIF(whereExpression != null, whereExpression)
                 .Select<DTSN_CanningPageDto>()
                 .ToPageListAsync(pageIndex, pageSize, totalCount);

                int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / pageSize.ObjToDecimal())).ObjToInt();
                return new PageModel<DTSN_CanningPageDto>()
                {
                    dataCount = totalCount,
                    pageCount = pageCount,
                    page = pageIndex,
                    PageSize = pageSize,
                    data = list
                };

                #endregion
            }

            {
                //var list = Db.Queryable<DTSN_Canning, DTSN_TongDao>
                //   ((c, td) => new object[] { JoinType.Left, c.Canning_TongDaoID == td.TD_ID })
                // //.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                // .WhereIF(whereExpression != null, whereExpression)
                // .GroupBy((c, td) => td.TD_Type)
                // .GroupBy((c, td) => Convert.ToDateTime(c.Canning_BeginTime).ToShortDateString())
                //   .Select((c, td) => new
                //   {
                //       date = Convert.ToDateTime(c.Canning_BeginTime).ToShortDateString(),
                //       type = td.TD_Type,
                //       sum = SqlFunc.AggregateSum(c.Canning_NetWeight)
                //   }).ToList();
            }



            {
                #region 多表
                {

                    RefAsync<int> totalCount = 0;
                    var list = await Db.Queryable<DTSN_Canning, DTSN_TongDao>

                       ((c, td) => new object[] { JoinType.Left, c.Canning_TongDaoID == td.TD_ID })

                     .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                     .WhereIF(whereExpression != null, whereExpression)
                     .Select<DTSN_CanningPageDto>()
                     .ToPageListAsync(pageIndex, pageSize, totalCount);

                    int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / pageSize.ObjToDecimal())).ObjToInt();




                    return new PageModel<DTSN_CanningPageDto>()
                    {
                        dataCount = totalCount,
                        pageCount = pageCount,
                        page = pageIndex,
                        PageSize = pageSize,
                        data = list
                    };
                }
                #endregion
            }

        }

        public Task<PageModel<DTSN_CanningPageDto>> QueryPageList(Expression<Func<DTSN_Canning, bool>> whereExpression, int intPageIndex = 1, int intPageSize = 20,
            string strOrderByFileds = null)
        {
            throw new NotImplementedException();
        }

        public async Task<List<ChartDto>> ReportChart(Expression<Func<DTSN_Canning, bool>> whereExpression)
        {
            var list = Db.Queryable<DTSN_Canning, DTSN_TongDao>
           ((c, td) => new object[] { JoinType.Left, c.Canning_TongDaoID == td.TD_ID })
           .WhereIF(whereExpression != null, whereExpression)

           .GroupBy((c, td) => td.TD_Type)
           .GroupBy((c, td) => c.Canning_Date)
             .Select((c, td) => new ChartDto
             {
                 date = Convert.ToDateTime(c.Canning_Date),
                 type = td.TD_Type ?? 0,
                 sum = SqlFunc.AggregateSum(c.Canning_NetWeight ?? 0)
             }).ToList();

            return list;
        }
 
    
    
    }
}

